package com.oscar.ehome.utils.util;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import static android.provider.Settings.System.DATE_FORMAT;

public class DateUtil {

    /**
     * @return 方法描述: 字符串转换成日期
     */
    public static Date StringformatDate(String param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = sdf.parse(param);// 把字符串转换成日期
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;

    }

    /**
     * @return 方法描述: 字符串转换成日期
     */
    public static Date getnianyue(String param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = null;
        try {
            date = sdf.parse(param);// 把字符串转换成日期
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;

    }

    /**
     * @return 获取当前时间 格式 yyyy-MM-dd
     */
    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        String times = "";
        try {
            date = new Date();
            times = sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return times;

    }

    /**
     * @return 获取当前时间 格式 yyyy-MM-dd
     */
    public static String gethourDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        String times = "";
        try {
            date = new Date();
            times = sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return times;

    }

    public static String getminDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date date = null;
        String times = "";
        try {
            date = new Date();
            times = sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return times;

    }

    public static long getTodayZero() {
        Date date = new Date();
        long l = 24 * 60 * 60 * 1000; // 每天的毫秒数
        // date.getTime()是现在的毫秒数，它 减去 当天零点到现在的毫秒数（
        // 现在的毫秒数%一天总的毫秒数，取余。），理论上等于零点的毫秒数，不过这个毫秒数是UTC+0时区的。
        // 减8个小时的毫秒值是为了解决时区的问题。
        return (date.getTime() - (date.getTime() % l) - 8 * 60 * 60 * 1000);
    }

    public static String getTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date d1 = new Date(time);
        return format.format(d1);
    }

    //孙磊 将当前时间装换为年月日时分秒毫秒并加3位随机数
    public static String getsuijima() {
        String suijima = "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmssSSS");
        Date curDate = new Date();//获取当前时间
        try {
            suijima = formatter.format(curDate);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }

//        Random r = new Random();
//        int suiji = r.nextInt(900) + 100;
//        suijima = suijima + suiji;
//        String s = "";
        return suijima;
    }

    /**
     * @param //参数描述 格式为yyyy年MM月dd日 HH:mm:ss
     * @return 返回值描述
     * @throws <异常类型> {@inheritDoc} 异常描述
     *                方法描述:比较两个时间的大小 time1>time2 返回false time1<time2 返回true
     */

    public static Boolean comparetimess(String time1, String time2) {
        java.text.DateFormat df = new SimpleDateFormat(
                "yyyy-MM");
        java.util.Calendar c1 = java.util.Calendar.getInstance();
        java.util.Calendar c2 = java.util.Calendar.getInstance();
        try {
            c1.setTime(df.parse(time1));
            c2.setTime(df.parse(time2));
            int result = c1.compareTo(c2);
            if (result <= 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {

        }
        return false;

    }

    /**
     * @param //参数描述 格式为yyyy年MM月dd日 HH:mm:ss
     * @return 返回值描述
     * @throws <异常类型> {@inheritDoc} 异常描述
     *                方法描述:比较两个时间的大小 time1>time2 返回false time1<time2 返回true
     */

    public static Boolean comparetimeymdhm(String time1, String time2) {
        java.text.DateFormat df = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm");
        java.util.Calendar c1 = java.util.Calendar.getInstance();
        java.util.Calendar c2 = java.util.Calendar.getInstance();
        try {
            c1.setTime(df.parse(time1));
            c2.setTime(df.parse(time2));
            int result = c1.compareTo(c2);
            if (result < 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {

        }
        return false;
    }

    // 获取今年的年份
    public static int getYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, 0);
        return c.get(Calendar.YEAR);
    }

    /**
     * 日期转换为字符串
     *
     * @param timeStr
     * @return
     */
    public static String dateToString(String timeStr) {
        // 判断是否是今年
        Date date = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date = df.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 日期逻辑
     *
     * @param dateStr 日期字符串
     * @return
     */
    public static String timeLogic(String dateStr) {
        Calendar calendar = Calendar.getInstance();
        calendar.get(Calendar.DAY_OF_MONTH);
        long now = calendar.getTimeInMillis();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        long past = calendar.getTimeInMillis();

        // 相差的秒数
        long time = (now - past) / 1000;

        StringBuffer sb = new StringBuffer();
        if (time > 0 && time <= 600) {//十分钟内
            return sb.append("刚刚").toString();
        } else if (time > 60 && time <= 3600) {//一个小时内
            return sb.append(time / 60 + "分钟前").toString();
        } else if (time >= 3600 && time <= 3600 * 36) {//36小时内
            return sb.append(time / 3600 + "小时前").toString();
        } else if (time >= 3600 * 72) {
            return dateToString(dateStr);
        }
        return dateToString(dateStr);
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static String strToDate(String strDate) {
        String date = "";
        if ("".equals(strDate) || strDate == null) {
            date = "暂无";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            date = dateToString(strDate);
        }

        return date;
    }

}
